package com.zzyk.main.utils;

import java.nio.charset.Charset;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * MD5签名工具类
 */
public class MD5Utils {
    public static String sign(String data) {
        try {
            return sign(data, Charset.forName("UTF-8"));
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String sign(String data, Charset charset) throws NoSuchAlgorithmException {
        MessageDigest digest = MessageDigest.getInstance("MD5");
        digest.update(data.getBytes(charset));
        byte[] arr = digest.digest();

        StringBuilder buffer = new StringBuilder();
        for (byte anArr : arr) {
            int i = anArr;
            if (i < 0) {
                i += 256;
            }
            if (i < 16) {
                buffer.append(0);
            }
            buffer.append(Integer.toHexString(i));
        }
        return buffer.toString();
    }
}
